package cn.mazexal.examapi.services;

import cn.mazexal.examapi.dataware.UserMsgMapper;
import cn.mazexal.examapi.models.converter.UserMsgConverter;
import cn.mazexal.examapi.models.db.UserMsgDO;
import cn.mazexal.examapi.models.vo.UserMsgVO;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author: Mazexal
 * Date: 2025/7/7
 */
@Service
public class UserMsgService {

    @Resource
    private UserMsgMapper userMsgMapper;

    @Resource
    private UserMsgConverter userMsgConverter;

    public List<UserMsgVO> getUserMsgVOList(Long userId, Long lastId, Integer pageSize) {
        if (lastId == null || lastId == 0) {
            lastId = Long.MAX_VALUE;
        }
        List<UserMsgDO> userMsgDOList = userMsgMapper.getUserMsgVOList(userId, lastId, pageSize);
        if (CollectionUtils.isEmpty(userMsgDOList)) {
            return Collections.EMPTY_LIST;
        }
        return userMsgDOList.stream().map(item -> userMsgConverter.toView(item)).toList();
    }

    public boolean readMsg(Long id) {
        return userMsgMapper.readMsg(id) > 0;
    }

    public boolean delMsg(Long id) {
        return userMsgMapper.delMsg(id) > 0;
    }
}
